﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using TextGlow.Control.Utilities;

namespace TextGlow.Control.Model
{
  public class TableRowProperties : ContentProperties
  {
    private string _alignment;
    private bool? _doNotPageSplit;
    private int? _gridColumnsAfter;
    private int? _gridColumnsBefore;
    private bool? _hideRowMarker;
    private int? _cellSpacing;
    private string _cellSpacingType;
    private bool? _tableHeader;
    private int? _height;
    private string _heightRule;
    private int? _preferredWidthAfter;
    private string _preferredWidthAfterType;
    private int? _preferredWidthBefore;
    private string _preferredWidthBeforeType;
    private ConditionalTableStyleType? _conditionalFormatting;

    public ConditionalTableStyleType? ConditionalFormatting
    {
      get { return _conditionalFormatting; }
      set { _conditionalFormatting = value; }
    }

    public string PreferredWidthBeforeType
    {
      get { return _preferredWidthBeforeType; }
      set { _preferredWidthBeforeType = value; }
    }

    public string PreferredWidthAfterType
    {
      get { return _preferredWidthAfterType; }
      set { _preferredWidthAfterType = value; }
    }

    public int? PreferredWidthBefore
    {
      get { return _preferredWidthBefore; }
      set { _preferredWidthBefore = value; }
    }

    public int? PreferredWidthAfter
    {
      get { return _preferredWidthAfter; }
      set { _preferredWidthAfter = value; }
    }

    public string HeightRule
    {
      get { return _heightRule; }
      set { _heightRule = value; }
    }

    public int? Height
    {
      get { return _height; }
      set { _height = value; }
    }

    public bool? TableHeader
    {
      get { return _tableHeader; }
      set { _tableHeader = value; }
    }

    public int? CellSpacing
    {
      get { return _cellSpacing; }
      set { _cellSpacing = value; }
    }

    public string CellSpacingType
    {
      get { return _cellSpacingType; }
      set { _cellSpacingType = value; }
    }

    public bool? HideRowMarker
    {
      get { return _hideRowMarker; }
      set { _hideRowMarker = value; }
    }

    public int? GridColumnsBefore
    {
      get { return _gridColumnsBefore; }
      set { _gridColumnsBefore = value; }
    }

    public int? GridColumnsAfter
    {
      get { return _gridColumnsAfter; }
      set { _gridColumnsAfter = value; }
    }

    public bool? DoNotPageSplit
    {
      get { return _doNotPageSplit; }
      set { _doNotPageSplit = value; }
    }

    public string Alignment
    {
      get { return _alignment; }
      set { _alignment = value; }
    }

    public TableRowProperties(Content parentContent, XElement element)
      : base(parentContent)
    {
      if (element == null)
        return;

      XNamespace ns = element.Name.Namespace;

      _alignment = XmlUtils.GetValue(element.Element(ns + "jc"));
      _doNotPageSplit = XmlUtils.GetBoolean(element.Element(ns + "cantSplit"));
      _gridColumnsAfter = XmlUtils.GetValue<int>(element.Element(ns + "gridAfter"));
      _gridColumnsBefore = XmlUtils.GetValue<int>(element.Element(ns + "gridBefore"));
      _hideRowMarker = XmlUtils.GetBoolean(element.Element(ns + "hidden"));
      _cellSpacing = XmlUtils.GetValue<int>(element.Element(ns + "tblCellSpacing"), "w");
      _cellSpacingType = XmlUtils.GetValue(element.Element(ns + "tblCellSpacing"), "type");
      _tableHeader = XmlUtils.GetBoolean(element.Element(ns + "tblHeader"));
      _height = XmlUtils.GetValue<int>(element.Element(ns + "trHeight"));
      _heightRule = XmlUtils.GetValue(element.Element(ns + "trHeight"), "hRule");
      _preferredWidthAfter = XmlUtils.GetValue<int>(element.Element(ns + "wAfter"), "w");
      _preferredWidthAfterType = XmlUtils.GetValue(element.Element(ns + "wAfter"), "type");
      _preferredWidthBefore = XmlUtils.GetValue<int>(element.Element(ns + "wBefore"), "w");
      _preferredWidthBeforeType = XmlUtils.GetValue(element.Element(ns + "wBefore"), "type");

      string conditionalValue = XmlUtils.GetValue(element.Element(ns + "cnfStyle"));
      if (conditionalValue != null)
        _conditionalFormatting = (ConditionalTableStyleType)BitUtils.ConvertToInt32(StringUtils.Reverse(conditionalValue));
    }

    private T GetValue<T>(Func<TableRowProperties, T> valueGetter)
    {
      throw new Exception("sdf");
    }
  }
}
